<template>
  <div>
    <f-search-form :more='true' @searchHandler='searchHandler'>
      <f-search-form-item label='订单编号' name='orderNo' />
      <f-search-form-item label='单据日期' type='range' name='beginDate' />
      <f-search-form-item label='部门' ref='dept' name='deptId' type='select' :data='api.hyorder.listByCompanyId' valueKey='deptId' label-key='deptName' />
    </f-search-form>
    <f-table ref='table' rowKey='orderId'
             :showSetting='false'
             setting-key='hy_order_tab1'
             :tableColumns='columns'
             :scroll='{x:true}'
             :multiple='false'
             :operator='operator' :params='queryParam' :api='api.hyorder' handler='hyOrderListHandler'>
      <span slot='sendType' slot-scope='text, record, index'>
        <a-tag color='#2db7f5' v-if='text==1'>现款</a-tag>
        <a-tag color='#FF0000' v-if='text==2'>预收款</a-tag>
        <a-tag color='#87d068' v-if='text==3'>资保金</a-tag>
        <a-tag color='purple' v-if='text==4'>全额资信</a-tag>
      </span>
      <span slot='deptType' slot-scope='text, record, index'>
        <a-tag color='#87d068' v-if='text=="ZS"'>招商</a-tag>
        <a-tag color='#108ee9' v-if='text=="ZY"'>自营</a-tag>
      </span>
      <span slot='sendState' slot-scope='text, record, index'>
        <a-tag color='#87d068' v-if='text==0'>未发货</a-tag>
        <a-tag color='purple' v-if='text==1'>已发货</a-tag>
      </span>
      <span slot='orderState' slot-scope='text, record, index'>
        <a-tag color='pink' v-if='text==0'>未提交</a-tag>
        <a-tag color='#87d068' v-if='text==1' @click='processImg(record)'>{{ record.taskName }}</a-tag>
        <a-tag color='#f50' v-if='text==2'>已审批</a-tag>
      </span>
      <span slot='action' slot-scope='text, record'>
        <template>
          <a @click='startProcess(record.orderId,"hyOrderStartProcessHandler")' v-if='record.orderState==0'>提交</a>
          <a @click='completeTask(record)' v-if="record.taskDefinitionKey&&record.taskDefinitionKey=='__initiator__'">提交</a>
          <span>
            <a-divider type='vertical' v-if="(record.taskDefinitionKey&&record.taskDefinitionKey=='__initiator__') ||record.orderState==0 "/>
            <a @click='$refs.drawer.showDrawer(record)' v-if='record.sourceType==2'>编辑</a>
            <a @click='$refs.drawer2.showDrawer(record)' v-if='record.sourceType==1'>编辑</a>
          </span>
          <a-divider type='vertical' />
            <a href='javascript:;' @click='$refs.orderReturnDrawer.showDrawer(record)'>详情</a>
          <a-divider type='vertical'></a-divider>
          </template>
          <a-dropdown>
            <a class='ant-dropdown-link'>更多<a-icon type='down' /></a>
            <a-menu slot='overlay'>
              <!--未提交审批的订单和撤销审批的订单才允许删除-->
              <a-menu-item v-if='record.orderState==0 && !record.processInstanceId && record.syncTag==0 && record.sourceType==2'>
                <a href='javascript:;' @click='$refs.table.del(record,{companyId:record.companyId,hostName:record.hostName})'>删除</a>
              </a-menu-item>
              <a-menu-item v-if='record.orderState==1 && record.taskDefinitionKey=="__initiator__"'>
                <a href='javascript:;' @click='deleteProcessInstance(record.processInstanceId,"hyOrderProcDeleteHandler")'>撤销审批</a>
              </a-menu-item>
              <a-menu-item v-if='record.orderState!=0'>
                <a href='javascript:;' @click='processImg(record)'>审批进度</a>
              </a-menu-item>
              <a-menu-item>
                <a href='javascript:;' @click='$refs.gxhtModal.open(record)'>下载购销合同</a>
              </a-menu-item>
              <a-menu-item v-if='record.companyId==1018 && record.customName=="湖北丹博瑞医药有限公司"'>
                <a href='javascript:;' @click='downloadGxhtCg(record)'>下载购销合同(采购)</a>
              </a-menu-item>
               <a-menu-item>
                <a href='javascript:;' @click='printOrder(record)'>发货单</a>
                  </a-menu-item>
                <a-menu-item v-if='(record.orderType=="CHA" && record.sourceType==1 && record.taskDefinitionKey=="__initiator__") || superAdmin'>
                <a href='javascript:;' @click='initOrder(record)' >同步订单</a>
                </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
    </f-table>
    <order-drawer ref='drawer' @success='onLoad'></order-drawer>
    <hy-order-drawer ref='drawer2' @success='onLoad'></hy-order-drawer>
    <hyorder-return-drawer ref='orderReturnDrawer'></hyorder-return-drawer>
    <print-modal ref='printModal'></print-modal>
    <process-image ref='processImg' v-if='processImageVisible' :visible.sync='processImageVisible' :processInstanceId='processInstanceId'></process-image>
    <gxht-modal ref='gxhtModal'></gxht-modal>
  </div>
</template>
<script>
import HyorderReturnDrawer from "./return/hyorder_return_drawer.vue"
import api from "../js/api"
import OrderDrawer from "./order_drawer"
import ProcessImage from "../../flowable/process/process_image"
import { processMixins } from "@/core/mixins/processMixins"
import PrintModal from "@/views/module/haiying/print/print_modal"
import GxhtModal from "./components/gxht_modal"
import HyOrderDrawer from "@/views/module/haiying/order/hy_order_drawer"

export default {
  name: "Tab1",
  components: {
    HyOrderDrawer,
    GxhtModal,
    OrderDrawer,
    HyorderReturnDrawer,
    ProcessImage,
    PrintModal
  },
  mixins: [processMixins],
  data() {
    let me = this
    //操作按钮
    let operator = []
     operator.push({
        name: "添加订单",
        icon: "plus",
        that: this,
        handler: (table, that) => {
          this.$refs.drawer.showDrawer({})
        }
      })
    return {
      api: Object.assign(api, this.api),
      //列表初始化查询参数
      queryParam: {},
      activeKey: "0",
      // 表头
      columns: [
        {
          title: "序号",
          width: "60px",
          fixed: "left",
          align: "center",
          customRender: (text, record, index) => index + 1
        },
        {
          title: "订单编号",
          align: "center",
          fixed: "left",
          dataIndex: "orderNo"
        },
        {
          title: "制单日期",
          align: "center",
          width: "100px",
          dataIndex: "createDate"
        },
        {
          title: "发货方式",
          align: "center",
          dataIndex: "sendType",
          width: "100px",
          scopedSlots: { customRender: "sendType" }
        },
        {
          title: "部门",
          align: "left",
          width: "120px",
          dataIndex: "deptName"
        },
        {
          title: "业务类型",
          align: "center",
          width: "80px",
          dataIndex: "deptType",
          scopedSlots: { customRender: "deptType" }
        },
        {
          title: "发货区域",
          align: "left",
          width: "80px",
          dataIndex: "sendArea"
        },
        {
          title: "单位名称",
          align: "left",
          ellipsis: true,
          width: "130px",
          dataIndex: "customName"
        },
        {
          title: "业务员",
          align: "left",
          width: "100px",
          dataIndex: "saleManName"
        },
        {
          title: "业务编码",
          align: "left",
          width: "120px",
          dataIndex: "businessCode"
        },

        {
          title: "发货金额",
          align: "left",
          width: "100px",
          dataIndex: "totalSaleAmount"
        },
        {
          title: "制单人",
          align: "left",
          width: "100px",
          dataIndex: "nickName"
        },
        {
          title: "创建日期",
          dataIndex: "createTime",
          width: 150,
          sorter: true
        },
        {
          title: "订单状态",
          align: "left",
          width: "120px",
          fixed: "right",
          dataIndex: "orderState",
          scopedSlots: { customRender: "orderState" }
        },
        {
          title: "操作",
          align: "center",
          dataIndex: "action",
          width: "190px",
          fixed: "right",
          scopedSlots: { customRender: "action" }
        }
      ],
      operator: operator,
      processImageVisible: false,
      processInstanceId: null
    }
  },
  created() {
  },
  methods: {
    onLoad() {
      this.$refs.table.refresh()
    },
    //重新提交
    completeTask(record) {
      this.http.confirm(this, {
        content: "确认提交吗?",
        url: this.api.tasktodo.complete,
        requestBody: true,
        data: {
          completeTaskHandler: "hyOrderTaskDataHandler",
          taskId: record.taskId
        },
        success: (me, data) => {
          this.$refs.table.refresh()
        }
      })
    },
    //申请撤销审批
    revokeProcess(order) {
      this.http.confirm(this, {
        content: "确认申请撤销审批吗?",
        url: api.hyorder.revokeProcess,
        data: {
          orderId: order.orderId
        },
        success: (me, data) => {
          this.$refs.table.refresh()
        }
      })
    },
    //申请撤销审批
    cancelRevokeProcess(order) {
      this.http.confirm(this, {
        content: "确认取消申请撤销审批吗?",
        url: api.hyorder.cancelRevokeProcess,
        data: {
          orderId: order.orderId
        },
        success: (me, data) => {
          this.$refs.table.refresh()
        }
      })
    },
    /**
     * 下载购销合同
     * @param record
     */
    downloadGxhtCg(record) {
      let env = process.env.NODE_ENV
      let url = "bus/export/gxht_cg"
      if (env == "development") {
        url = "../../../base/bus/export/gxht_cg"
      }
      this.http.exportExcel(this, {
        url: url,
        data: {
          orderId: record.orderId
        },
        success: function(me, data) {
        }
      })
      this.visible = false
    },
    printOrder(record) {
      this.$refs["printModal"].open(record.orderId, 1)
    },
    changeHandler(activeKey) {
      this.activeKey = activeKey
      this.$refs.table.refresh(true, { activeKey: this.activeKey })
    },
    processImg(order) {
      this.processInstanceId = order.processInstanceId
      this.processImageVisible = true
    },
    initOrder(record) {
      this.http.confirm(this, {
        content: "确认重新同步吗?",
        url: api.hyorder.syncOrder,
        data: {
          orderNo: record.orderNo
        },
        success: (me, data) => {
          this.$refs["table"].refresh(false)
        }
      })
    }
  }
}
</script>
